package com.river.boot.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.river.boot.biz.model.form.ProductCategoryForm;
import com.river.boot.biz.model.query.ProductCategoryQuery;
import com.river.boot.biz.model.vo.ProductCategoryVO;
import com.river.boot.biz.service.ProductCategoryService;
import com.river.boot.common.base.Option;
import com.river.boot.common.result.PageResult;
import com.river.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品分类前端控制层
 *
 * @author JiangCheng Xiang
 * @since 2025-04-24 17:12
 */
@Tag(name = "13.菜品分类接口")
@RestController
@RequestMapping("/api/v1/productCategorys")
@RequiredArgsConstructor
public class ProductCategoryController {

    private final ProductCategoryService productCategoryService;

    @Operation(summary = "菜品分类分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('biz:productCategory:query')")
    public PageResult<ProductCategoryVO> getProductCategoryPage(ProductCategoryQuery queryParams) {
        IPage<ProductCategoryVO> result = productCategoryService.getProductCategoryPage(queryParams);
        return PageResult.success(result);
    }

    @ApiOperationSupport(order = 2)
    @Operation(summary = "分类下拉列表")
    @GetMapping("/options")
    public Result<List<Option<Long>>> getCategoryOptions() {
        List<Option<Long>> list = productCategoryService.listCategoryOptions();
        return Result.success(list);
    }

    @Operation(summary = "新增菜品分类")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('biz:productCategory:add')")
    public Result<Void> saveProductCategory(@RequestBody @Valid ProductCategoryForm formData) {
        boolean result = productCategoryService.saveProductCategory(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取菜品分类表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('biz:productCategory:edit')")
    public Result<ProductCategoryForm> getProductCategoryForm(
            @Parameter(description = "菜品分类ID") @PathVariable Long id
    ) {
        ProductCategoryForm formData = productCategoryService.getProductCategoryFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改菜品分类")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('biz:productCategory:edit')")
    public Result<Void> updateProductCategory(
            @Parameter(description = "菜品分类ID") @PathVariable Long id,
            @RequestBody @Validated ProductCategoryForm formData
    ) {
        boolean result = productCategoryService.updateProductCategory(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除菜品分类")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('biz:productCategory:delete')")
    public Result<Void> deleteProductCategorys(
            @Parameter(description = "菜品分类ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = productCategoryService.deleteProductCategorys(ids);
        return Result.judge(result);
    }
}
